package org.geometerplus.zlibrary.core.filesystem;

import android.util.Log;

import com.example.administrator.fangbook.read.library.constants.GlobalConstants;
import com.example.administrator.fangbook.utils.FileUtils;

import org.geometerplus.fbreader.filetype.FileTypeTxt;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * txt文件
 */
public final class ZLTxtFile extends ZLFile {

    public static final String ExtentionName = "txt";


    private String myBookId;

    private String myBookName;

    private String myChapterId;

    private String mChapterName;

    /**
     * 完整路径
     */
    protected String myFullPath;


    public static ZLTxtFile createByUri(String uri) {
        if (uri.startsWith(FileTypeTxt.FileMode)) {
            String prefix = FileTypeTxt.FileMode + GlobalConstants.LOCAL_BOOK_PATH + "/";
            String sub = uri.substring(prefix.length());
            String bookId = sub.substring(0, sub.indexOf("/"));
            String chapterId = sub.substring(sub.indexOf("/") + 1);
            ZLTxtFile zfile = new ZLTxtFile(bookId, "", chapterId, "");
            zfile.setBookId(bookId);
            zfile.myFullPath = null;
            return zfile;
        }
        return null;
    }

    public ZLTxtFile(String fullPath, String myBookName, String myChapterId, String mChapterName) {
        myFullPath = fullPath;
        //判断文件是否存在
        boolean bo = FileUtils.isFileExist(myFullPath);
        if (bo) {
            setBookId(fullPath.hashCode() + "");
        } else {
            setBookId(fullPath);
        }
        //设定扩展名
        setExtension(ExtentionName);
        //设定图书名称
        setBookName(myBookName);
        //设定章节id
        setChapterId(myChapterId);
        //设定章节名称
        setChapterName(mChapterName);

    }

    public String getFullPath() {
        return myFullPath;
    }

    @Override
    public boolean exists() {
        File f = new File(getPath());
        return f.isFile() && f.exists();
    }

    @Override
    public InputStream getInputStream() throws IOException {

        return null;
    }

    @Override
    public String getLongName() {
        if (getFullPath() == null) {
            return GlobalConstants.LOCAL_BOOK_PATH + "/" + myBookId + "." + ExtentionName;
        } else {
            return getFullPath() + "." + ExtentionName;
        }
    }

    @Override
    public ZLFile getParent() {
        return null;
    }

    @Override
    public String getPath() {
        return GlobalConstants.LOCAL_BOOK_PATH + "/" + getBookId() + "/" + getChapterId();
    }

    @Override
    public ZLPhysicalFile getPhysicalFile() {
        return null;
    }

    @Override
    public boolean isDirectory() {
        return false;
    }

    @Override
    public long size() {
        return 0;
    }

    public void setBookId(String myBookId) {
        this.myBookId = myBookId;
    }

    @Override
    public String getBookId() {
        return myBookId;
    }

    @Override
    public String getBookName() {
        return myBookName;
    }

    public void setBookName(String myBookName) {
        this.myBookName = myBookName;
    }

    public void setChapterId(String chapterId) {
        myChapterId = chapterId;
    }

    @Override
    public String getChapterId() {
        return myChapterId;
    }

    @Override
    public String getChapterName() {
        return mChapterName;
    }

    public void setChapterName(String name) {
        mChapterName = name;
    }

    @Override
    public String getFileMode() {
        return FileTypeTxt.FileMode;
    }

}
